package com.ynsj.config.swagger;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author 86130
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    private SwaggerProperties swaggerProperties;

    public SwaggerAutoConfiguration(SwaggerProperties swaggerProperties){
        this.swaggerProperties = swaggerProperties;
    }

    /**
     * swagger的全局配置对象
     * @return
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                //配置接口信息介绍
                .apiInfo(getApiInfo())
                .select()
                //配置要给哪个包下的接口生成文档
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                //表示要生成文档的接口路径
                .paths(PathSelectors.any())
                .build()
                //配置鉴权需要哪些参数
                .securitySchemes(setSecuritySchemes())
                //配置有哪些请求需要携带token
                .securityContexts(setSecurityContexts());

    }

    /**
     * 配置有哪些请求需要携带token
     * @return
     */
    private List<SecurityContext> setSecurityContexts() {
        return Collections.singletonList(new SecurityContext(
                Collections.singletonList(new SecurityReference(
                        "Authorization",new AuthorizationScope[]{
                                new AuthorizationScope("global","accessResource")})),PathSelectors.any()));
    }

    /**
     * 配置鉴权需要哪些参数
     * @return
     */
    private List<SecurityScheme> setSecuritySchemes() {
        return Collections.singletonList(new ApiKey("Authorization", "Authorization", "Authorization"));
    }


    /**
     * 配置接口信息介绍
     * @return
     */
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
                //API标题
                .title(swaggerProperties.getTitle())
                //API描述
                .description(swaggerProperties.getDescription())
                //API版本号
                .version(swaggerProperties.getVersion())
                //团队服务地址
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .build();

    }
}
